package com.wanpinghui.wphlibrary.webapp;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.gson.Gson;
import com.wanpinghui.wphlibrary.model.product.bean.ProductBean;
import com.wanpinghui.wphlibrary.model.product.bean.ProductMgrImageBean;
import com.yunshl.yunshllibrary.utils.LogUtils;

import java.util.List;
import java.util.Map;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/6/20
 ***/

public class WPHWebView extends WebView {
    private static final String TAG = "HDWebView";

    private boolean isNoError =true;

    private String baseUrl;


    public WPHWebView(Context context) {
        super(context);
        init();
    }

    public WPHWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WPHWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public void addJsMethod(JSMethod jsMethod) {
        addJavascriptInterface(jsMethod, "wanpinghui");
    }


    private void init() {
        setLayerType(View.LAYER_TYPE_HARDWARE,null);
        WebSettings wSet = getSettings();
        wSet.setJavaScriptEnabled(true);
        wSet.setDomStorageEnabled(true); // 开启 DOM storage API 功能
        wSet.setDatabaseEnabled(true);   //开启 database storage API 功能
        wSet.setAppCacheEnabled(true);//开启 Application Caches 功能

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //支持https网页里引用http的资源
            wSet.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                Log.d(TAG, "webview =====>onConsoleMessage"+consoleMessage.message());
                return super.onConsoleMessage(consoleMessage);
            }

        });

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public WPHWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public void goWebBack(){
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            evaluateJavascript("javascript:app_goback()",null);
        }else {
            loadUrl("javascript:app_goback()");
        }
    }

    public void setPhotos(List<ProductMgrImageBean> photos) {
        if (photos == null || photos.size() == 0)return;
        Gson gson = new Gson();
        String goodsStr = gson.toJson(photos);
        LogUtils.w("调用h5设置图片方法:"+goodsStr);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            evaluateJavascript("javascript:setImgs(" + goodsStr + ")",null);
        }else {
            loadUrl("javascript:setImgs(" + goodsStr + ")");
        }
    }

    public void setUploadPhotos(List<ProductMgrImageBean> photos) {
        if (photos == null || photos.size() == 0)return;
        Gson gson = new Gson();
        String goodsStr = gson.toJson(photos);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            evaluateJavascript("imgUploaded("+goodsStr+")",null);
        }else {
            loadUrl("javascript:imgUploaded("+goodsStr+")");
        }
    }

    public void setCartCount(int count) {
        loadUrl("javascript:updateCartNum("+count+")");
    }

    class WebParams{
        int type;
        WebParamData param;
    }

    public class WebParamData{
        public int pick_num;
        public ProductBean p_json;
        public List<ProductMgrImageBean> pics;
    }

    public interface OnErrorListener{
        void onError();
        void onSuccess();
    }

}
